package com.diana.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.diana.aop.Mylog;
import com.diana.common.*;
import com.diana.service.ManagerMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author anzhi
 * @since 2019-04-21
 */
@RestController
public class ManagerMainController {

    @Autowired
    private ManagerMainService managerMainService;

    /**
     * 添加管理员
     * @param map
     * @return
     */
    @Mylog(remark = "添加管理员")
    @RequestMapping("/admin/managerMain/addManager")
    public String addManager(@RequestBody Map<String,Object> map){
        return managerMainService.addManager(map);
    }

    /**
     * 修改基本信息
     * @param map
     * @return
     */
    @Mylog(remark = "修改管理员信息")
    @RequestMapping({"/admin/managerMain/updateManager","/admin/managerMain/updateCurrentManager"})
    public String updateBaseInfo(@RequestBody Map<String,Object> map){
        return managerMainService.updateBaseInfo(map);
    }

    /**
     * 修改管理员状态
     * @param map
     * @return
     */
    @Mylog(remark = "修改管理员状态")
    @RequestMapping("/admin/managerMain/deleteManager")
    public String updateStatus(@RequestBody Map<String,Object> map){
        return managerMainService.updateStatus(map);
    }

    /**
     * 获取管理员信息
     * @param map
     * @return
     */
    @RequestMapping("/admin/managerMain/getManagerInfo")
    public String getManagerInfo(@RequestBody Map<String,Object> map){
        return managerMainService.getManagerInfo(map);
    }

    /**
     * 获取管理员列表
     * @param map
     * @return
     */
    @RequestMapping("/admin/managerMain/getManagerList")
    public String getManagerList(@RequestBody Map<String,Object> map){
        return managerMainService.getManagerList(map);
    }

    /**
     * 管理员登录
     * @param map
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestBody Map<String,Object> map){
        return managerMainService.login(map);
    }

    /**
     * 获取当前用户信息
     * @param managerId
     * @return
     */
    @RequestMapping("/admin/managerMain/getCurrentManagerInfo")
    public String getCurrentManagerInfo(@RequestHeader("managerId")String managerId){
        return managerMainService.getCurrentManagerInfo(managerId);
    }



    @RequestMapping("/test")
    public String test(@RequestParam(value = "file",required = false)MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String path = "/diana/manager/image/";
        String suffix  = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String fileName = IDUtil.getId()+"."+suffix;
        String url = TxCosUtils.upload(path, file.getInputStream(), fileName);
        if(!Utils.isNullOrEmpty(url)){
            return Utils.message("0000","上传成功",url);
        }
        return Utils.message("0001","上传失败",null);
    }

    @Value("${file.download.path}")
    private String EXPORT_PATH;

    @RequestMapping("/export")
    public String export() throws IOException {

        List list = managerMainService.selectMaps(Condition.create().ne("status", StatusEnums.CommonStatus.删除));

        File file = ExcelUtils.exportTest(EXPORT_PATH, list);

        String path = "/diana/";
        String url = TxCosUtils.upload(path, file);
        if(!Utils.isNullOrEmpty(url)){
            return Utils.message("0000","操作成功",url);
        }
        return Utils.message("0001","操作失败",null);
    }
}

